import { getSystemInfoSync } from '../crossplatform/apiservice/systemInfo'

const { windowHeight, windowWidth } = getSystemInfoSync()
const dpr = windowWidth / 750
let globalData = {
  env: 'daily', // daily gray online
  windowHeight,
  dpr,
  IMData: {},
  nim: {},
  settlementMethod: [
    {
      name: '按天结算',
      short: '天结',
      unit: '元/天',
      value: 0
    },
    {
      name: '按周结算',
      short: '周结',
      unit: '元/周',
      value: 1
    },
    {
      name: '按月结算',
      short: '月结',
      unit: '元/月',
      value: 2
    },
    {
      name: '按次结算',
      short: '次结',
      unit: '元/次',
      value: 3
    }
  ],
  unitMethod: [ // 结算方式
    {
      unit: '元',
      name: '小时',
      value: 0
    },
    {
      unit: '元',
      name: '天',
      value: 1
    },
    {
      unit: '元',
      name: '周',
      value: 2
    },
    {
      unit: '元',
      name: '月',
      value: 3
    },
    {
      unit: '元',
      name: '次',
      value: 4
    }
  ],
  education: [
    { value: 1, name: '中专' },
    { value: 2, name: '专科' },
    { value: 3, name: '本科' },
    { value: 4, name: '硕士' },
    { value: 5, name: '博士' }
  ],
  workNature: ['兼职', '全职', '实习'],
  storageKey: {
    chooseRole: {
      key: 'chooseRole'
    }
  },
  blockWords: /微信|加微|V信|威信|QQ|VX|WX|企鹅|\+V|weixin|扣扣|保安|安保|模特|APP推广|医药|充场|撑场|试依|试衣|AP|App|信用卡|网拍|推广|摄影|主播|\d{6,}/i
}

export function setGlobalData(key, val) {
  globalData[key] = val
}

export function getGlobalData(key) {
  if (key) {
    return globalData[key]
  } else {
    return globalData
  }
}

export function getGlobalDeepData(key, id) {
  if (key) {
    const data = globalData[key]
    let second = null
    let first = data.find(e => {
      second = e.children.find(f => {
        return f.value == id
      })
      return !!second
    })
    return second ? first.key + second.name : ''
  } else {
    return null
  }
}

export function getGlobalDeepindexId(key, id) {
  if (key) {
    const data = globalData[key]
    let second = null
    let first = data.findIndex(e => {
      second = e.children.findIndex(f => {
        return f.value == id
      })
      return second >= 0
    })
    return first >= 0 && second >= 0 ? [first, second] : []
  } else {
    return []
  }
}

export function getGlobalDeepIndex(key, name) {
  if (key) {
    const data = globalData[key]
    let second = null
    let first = data.findIndex(e => {
      second = e.children.findIndex(f => {
        return e.key + f.name == name
      })
      return second >= 0
    })
    return first >= 0 && second >= 0 ? [first, second] : []
  } else {
    return []
  }
}

const environmentJudge = () => {
  // 判断当前环境  daily gray online
  const { env } = globalData
  switch (env) {
    case 'daily':
      // 测试
      globalData['API_URL'] = 'http://dailymini.renzhichu.cn'
      globalData['API_URL2'] = 'http://dailymini.renzhichu.cn'
      globalData['IMG_URL'] = 'http://image.cdn.renzhichu.cn/'
      globalData['API_IM'] = 'http://dailymini.renzhichu.cn'

      globalData['IM_ID'] = 'test'

      globalData['IMData'] = {
        appKey: '78572d9cddadfc6a00d98398cc0a9781'
      }
      break
    case 'gray':
      // 预发布
      globalData['API_URL'] = 'http://testmini.renzhichu.cn'
      globalData['API_URL2'] = 'http://testmini.renzhichu.cn'
      globalData['IMG_URL'] = 'http://image.cdn.renzhichu.cn/'
      globalData['API_IM'] = 'http://testmini.renzhichu.cn'

      globalData['IM_ID'] = 'pre'

      globalData['IMData'] = {
        appKey: '78572d9cddadfc6a00d98398cc0a9781'
      }
      break
    case 'online':
      // 正式
      globalData['API_URL'] = 'https://mini.renzhichu.cn'
      globalData['API_URL2'] = 'https://mini.renzhichu.cn'
      globalData['IMG_URL'] = 'http://image.cdn.renzhichu.cn/'
      globalData['API_IM'] = 'https://mini.renzhichu.cn'

      globalData['IM_ID'] = ''

      globalData['IMData'] = {
        appKey: '78572d9cddadfc6a00d98398cc0a9781'
      }
      break
  }
}
environmentJudge()
